package com.fancy.service.impl;

import com.fancy.emums.StatusEnum;
import com.fancy.exception.SellException;
import com.fancy.po.ProductInfo;
import com.fancy.repository.ProductInfoRepository;
import com.fancy.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品实现
 * Created by FancyFong on 6/9/2017.
 */
@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoRepository repository;

    @Override
    public ProductInfo findOne(String productId) {
        ProductInfo productInfo = repository.findOne(productId);

        if(productInfo==null){
            throw new SellException(StatusEnum.ERROR);
        }

        return productInfo;
    }

    @Override
    public List<ProductInfo> findUpAll() {
        //StatusEnum.UP.getCode()
        return repository.findByProductStatus(StatusEnum.UP.getCode());
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return repository.save(productInfo);
    }
}
